package com.chnbst.bstyun.controller.admin;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.GoodsType;
import com.chnbst.bstyun.model.pojo.IndexModule;
import com.chnbst.bstyun.model.vo.IndexModuleVo;
import com.chnbst.bstyun.service.GoodsTypeService;
import com.chnbst.bstyun.service.IndexModuleService;


/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class IndexGroomAdminController {
	
	@Autowired
	private IndexModuleService indexModuleService;
	
	/**
	 * 首页推荐设置管理界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/indexGroomAdmin",method=RequestMethod.GET)
	public ModelAndView indexGroomAdmin()throws Exception{
		List<IndexModuleVo> indexModuleList = indexModuleService.selectGrid();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/admin/productManager/indexGroomManager");
		mav.addObject("indexModuleList", indexModuleList);
		return mav;
	}
	
	/**
	 * @return 新增数据
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/indexGroomAdmin/saveIndexModule",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveIndexModule(String id,String name,Integer num,String categoryid,HttpServletRequest request)throws Exception{
		
		boolean isSuccess = false;
		
		IndexModule indexModule = new IndexModule();
		indexModule.setModuleId(id);
		indexModule.setModuleName(name);
		indexModule.setModuleNum(num);
		indexModule.setTypeId(categoryid);
		indexModule.setCreateTime(new Date());
		indexModule.setIsDelete(0);
		indexModule.setIsShow(0);
		
		if("".equals(indexModule.getModuleId())){
			isSuccess = indexModuleService.insertIndexModule(indexModule);
		}else{
			isSuccess = indexModuleService.updateIndexModule(indexModule);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		
		return map;
		
	}
	
	/**
	 * @return 查询单条
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/indexGroomAdmin/selectIndexModuleInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectIndexModuleInfo(String id,HttpServletRequest request)throws Exception{
		boolean isOk = true;
		IndexModuleVo indexModule = indexModuleService.selectIndexModuleById(id);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(indexModule == null){
			isOk = false;
		}
		map.put("isOk", isOk);
		map.put("indexModule", indexModule);
		
		return map;
		
	}
	
	/**
	 * @return 根据ID 删除
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/indexGroomAdmin/deleteByModuleId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteByModuleId(String id,HttpServletRequest request)throws Exception{
		
		boolean isDelete = true;
		
		IndexModule indexModule = indexModuleService.selectIndexModuleById(id);
		if(indexModule.getIsShow() == 0){
			indexModule.setIsDelete(1);
			isDelete = indexModuleService.updateIndexModule(indexModule);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isDelete", isDelete);
		
		return map;
		
	}
	
	/**
	 * @return 根据ID 显示或者隐藏
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/indexGroomAdmin/selectIndexModuleById",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectIndexModuleById(String id,Integer state,HttpServletRequest request)throws Exception{
		
		boolean isShow = true;
		
		IndexModule indexModule = indexModuleService.selectIndexModuleById(id);
		indexModule.setIsShow(state);
		isShow = indexModuleService.updateIndexModule(indexModule);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", isShow);
		
		return map;
		
	}
}
